package com.zjc.stock.strategy;

import com.zjc.stock.StockExchange;
import com.zjc.stock.model.Account;
import com.zjc.stock.model.FeeDetail;
import com.zjc.stock.model.Stock;

import java.math.BigDecimal;

/**
 * ETF最优做T策略（不收过户费、印花税）
 */
public class ETFTStrategy {


    public static void main(String[] args) {
        Account account = new Account(0.000235, 0.0);
        StockExchange.account = account;
        StockExchange.stampTaxRate = 0.0;
        Stock stock = new Stock(1.147);

        Double minDifference = 0.001;  // 做t差额
        int minI = 5;  //最小手数
        Double maxMoney = 50000.00;
        Double preProfit = 5.00;

        Double minMoney = StockExchange.minCommission / account.getCommissionRate();
        long minStroke = (long) Math.ceil(StockExchange.minCommission / account.getCommissionRate() / stock.getPrice() / 100);
        System.out.println(String.format("超过最小佣金金额：%f 手数：%d", minMoney, minStroke));
        System.out.println();

        for (int i = minI; i < Integer.MAX_VALUE; i++) {
            FeeDetail sell = StockExchange.sell(StockExchange.SINGLE_STROKE * i, stock.getPrice() + minDifference);
            FeeDetail buy = StockExchange.buy(StockExchange.SINGLE_STROKE * i, stock.getPrice());

            Double difference = StockExchange.SINGLE_STROKE * i * minDifference;
            Double profit = difference - sell.getTotalFee() - buy.getTotalFee();
            if (profit > preProfit && buy.getCommission() > StockExchange.minCommission) {
                System.out.println("做T差额：" + minDifference);
                System.out.println("手数：" + i);
                System.out.println();

                System.out.println("买入金额：" + sell.getAmount());
                System.out.println("卖出金额：" + buy.getAmount());
                System.out.println("差额：" + difference);
                System.out.println("净利：" + profit);
                System.out.println();

                sell.print();
                System.out.println();
                buy.print();
                break;
            }

            if (i > 1000 || buy.getAmount() > maxMoney) {
                BigDecimal b = new BigDecimal("0.01");
                BigDecimal a = new BigDecimal(minDifference.toString());
                BigDecimal sum = b.add(a);
                minDifference = sum.doubleValue();
                i = 1;
            }
        }


    }
}
